package com.bysj.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bysj.entity.News;
import com.bysj.fenye.pagevo.NewsPageVO;
import com.bysj.service.NewsService;
import com.bysj.service.impl.NewsServiceImpl;

public class NewsServlet extends HttpServlet {
	private NewsPageVO pagevo=new NewsPageVO(10);
	private NewsService ns=new NewsServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m=request.getParameter("m");
		String method=request.getParameter("method");
		if("pre".equals(method)){
			this.pagevo.setPrevious();
		}else if("next".equals(method)){
			this.pagevo.setNext();
		}else if("last".equals(method)){
			this.pagevo.setLast();
		}else if("index".equals(method)){
			String ind=request.getParameter("ind");
			this.pagevo.setIndex(Integer.parseInt(ind.trim()));
		}else{
			this.pagevo.setFirst();
		}
		if("AllNews".equals(m)){
			request.getSession().setAttribute("nlist", pagevo);
			request.getRequestDispatcher("../news/allnews.jsp").forward(request, response);
		}else if("DetailsNews".equals(m)){
			Integer id=Integer.parseInt(request.getParameter("id"));
			News n=new News();
			n.setId(id);
			News news=ns.queryNews(n);
			request.getSession().setAttribute("newsd", news);
			request.getRequestDispatcher("../news/detailsnews.jsp").forward(request, response);
		}else if("addNews".equals(m)){
			String title=request.getParameter("title");
			String content=request.getParameter("FCKeditor");
			String time=request.getParameter("time");
			String pic=request.getParameter("pic");
			String author=request.getParameter("author");
			String source=request.getParameter("source");
			String editor=request.getParameter("editor");
			//System.out.println(content);
		//	System.out.println(title+"------"+content+"----"+time+"===="+pic+"===="+author+"---"+source+"---"+editor);
			News n=new News();
			n.setTitle(title);
			n.setContent(content);
			n.setTime(time);
			n.setPic(pic);
			n.setAuthor(author);
			n.setSource(source);
			n.setEditor(editor);
			ns.addNews(n);
			request.getSession().setAttribute("nlist", pagevo);
			request.getRequestDispatcher("../news/allnews.jsp").forward(request, response);
		}else if("DelNews".equals(m)){
			Integer id=Integer.parseInt(request.getParameter("id"));
			News n=new News();
			n.setId(id);
			ns.delNews(n);
			request.getSession().setAttribute("nlist", pagevo);
			request.getRequestDispatcher("../news/allnews.jsp").forward(request, response);
		}else if("uploadPic".equals(m)){
			request.getSession().setAttribute("uploadpicid", request.getParameter("id"));
			request.getRequestDispatcher("../news/uploadpic.jsp").forward(request, response);
		}
	}
	public NewsPageVO getPagevo() {
		return pagevo;
	}
	public void setPagevo(NewsPageVO pagevo) {
		this.pagevo = pagevo;
	}
	public NewsService getNs() {
		return ns;
	}
	public void setNs(NewsService ns) {
		this.ns = ns;
	}

}
